package labox.innovation.gameserver.network.clientpackets;

import java.util.ArrayList;
import java.util.List;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.FriendNewsFactory;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

public final class RequestFriendInvite extends L2GameClientPacket {
	private static final String _C__5E_REQUESTFRIENDINVITE = "[C] 5E RequestFriendInvite";

	//private static Logger _log = Logger.getLogger(RequestFriendInvite.class.getName());

	private long objId;

	@Override
	protected void readImpl() {
		objId = readLong();
	}

	@Override
	protected void runImpl() {
		final FuzePcInstance activeChar = getClient().getActiveChar();

		if (activeChar == null)
			return;

		final FuzePcInstance friend = FuzeWorld.getInstance().getPlayer(objId);
		String _name = null;
		SystemMessage sm = null;
		if (friend != null) {
			_name = friend.getName();
		}
		// can't use friend invite for locating invisible characters
		if (friend == null || friend.isOnline() == 0 || friend.getAppearance().getInvisible()) {
			//Target is not found in the game.
			sm = new SystemMessage(SystemMessageId.THE_USER_YOU_REQUESTED_IS_NOT_IN_GAME);
			activeChar.sendPacket(sm);
			return;
		} else if (friend == activeChar) {
			//You cannot add yourself to your own friend list.
			sm = new SystemMessage(SystemMessageId.YOU_CANNOT_ADD_YOURSELF_TO_OWN_FRIEND_LIST);
			sm.addCharName(friend);
			activeChar.sendPacket(sm);
			return;
		} else if (activeChar.getRace() != friend.getRace()) {
			sm = new SystemMessage(SystemMessageId.NO_SAME_RACE_CAN_NOT_BECOME_FRIENDS);
			activeChar.sendPacket(sm);//不是一个种族不能添加为好友
			return;
		} else if (activeChar.getFriendInstance().getFriendList().size() >= Config.FRIENDSLIMIT) {
			sm = new SystemMessage(SystemMessageId.YOUR_FRIEND_LIST_LIMIT);//自己好友已达上限
			sm.addCharName(friend);
			activeChar.sendPacket(sm);
			return;
		}
		/*		
		else if(friend.getFriendInstance().getFriendList().size()>=Config.FRIENDSLIMIT){
			sm = new SystemMessage(SystemMessageId.OTHER_FRIEND_List__LIMIT);
			sm.addCharName(friend);//对方好友已达上限
			activeChar.sendPacket(sm);
			return;
		}
		else if (BlockList.isBlocked(activeChar, friend))
		{
			sm = new SystemMessage(SystemMessageId.BLOCKED_C1);
			sm.addCharName(friend);
			activeChar.sendPacket(sm);
			return;
		}
		else if (BlockList.isBlocked(friend, activeChar))
		{
			activeChar.sendMessage("You are in target's block list.");
			return;
		}
		*/
		if (activeChar.getFriendInstance().isContains(friend.getObjectId())) {
			// Player already is in your friendlist
			sm = new SystemMessage(SystemMessageId.S1_ALREADY_IN_FRIENDS_LIST);
			sm.addString(_name);
			activeChar.sendPacket(sm);
			return;
		}
		/*
		if (!friend.isProcessingRequest())
		{
			// requets to become friend
			activeChar.onTransactionRequest(friend);
			sm = new SystemMessage(SystemMessageId.YOU_REQUESTED_C1_TO_BE_FRIEND);
			sm.addString(_name);
			FriendAddRequest ajf = new FriendAddRequest(long friendId);
			friend.sendPacket(ajf);
			}
		else
		{
			sm = new SystemMessage(SystemMessageId.C1_IS_BUSY_TRY_LATER);
			sm.addString(_name);
		}
		
		activeChar.sendPacket(sm);
		 * */
	
		activeChar.getFriendInstance().addFriend(friend);
		sm = new SystemMessage(SystemMessageId.S1_ADDED_TO_FRIENDS);
		sm.addCharName(friend);
		activeChar.sendPacket(sm);
		sendFriendNewsMessage(activeChar, friend);

	}

	private void sendFriendNewsMessage(FuzePcInstance instance, FuzePcInstance friends) {
		if (instance == null) {
			return;
		}
		SystemMessage sm = null;
		List<Long> _temp = null;
		if (friends.getFriendInstance().isContains(instance.getObjectId())) {
			_temp = new ArrayList<Long>();
			_temp.add(friends.getObjectId());
		}
		instance.getFriendInstance().getNewsManager().sendMessage(FriendNewsFactory.creatAddFriendsMessage(instance, friends), _temp);
		sm=new SystemMessage(SystemMessageId.S1_WILL_ADD_TO_HIS_FRIENDS_LIST);
		sm.addCharName(instance);
		friends.sendPacket(sm);
		friends.getFriendInstance().addFriendNews(FriendNewsFactory.creatAddMeMessage(instance));
		List<Long> temp = friends.getFriendInstance().getHeroesId();
		if (!temp.contains(instance.getObjectId())) {
			temp.add(instance.getObjectId());
		}
	}

	@Override
	public String getType() {
		return _C__5E_REQUESTFRIENDINVITE;
	}
}